package jaux.tank.generator.jd.components;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.TableCellEditor;  
  
public class JdTextAreaCellEditor extends AbstractCellEditor implements TableCellEditor {  
	
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JDialog dialog;
    private JTextArea textArea;  
    private JScrollPane scrollPane;  
    private JButton button;  
    private Object value;  
  
    public JdTextAreaCellEditor() {  
    	dialog = new JDialog();
    	dialog.setModal(true);
    	dialog.setLocationRelativeTo(null);
		dialog.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				stopCellEditing();
				dialog.setVisible(false);
			}
		});
		dialog.setLayout(new BorderLayout(10,0));
        textArea = new JTextArea(4,0);  
        textArea.setLineWrap(true);  
        textArea.setWrapStyleWord(true);  
        
  
        scrollPane = new JScrollPane(textArea);  
        scrollPane.setPreferredSize(new Dimension(200, 100));  
        dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);
  
        button = new JButton("Done");  
        button.addActionListener(new ActionListener() {  
            @Override  
            public void actionPerformed(ActionEvent e) {  
                stopCellEditing();  
                dialog.setVisible(false);
            }  
        });  
        dialog.getContentPane().add(button, BorderLayout.SOUTH);
        dialog.pack();
        textField = new JTextField();
        textField.addFocusListener(new java.awt.event.FocusAdapter() {
	        @Override
	        public void focusGained(FocusEvent e) {
	        	super.focusGained(e);
	        	dialog.setVisible(true);
	        	textArea.requestFocus();
	        }
        });
    }  
  
    @Override  
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {  
        this.value = value;  
        String text = (value == null) ? "" : value.toString();
		textArea.setText(text);  
        textField.setText(text);
        dialog.setLocationRelativeTo(table);
        dialog.setTitle(table.getColumnName(column));
        return textField;  
    }  
  
    
    @Override  
    public Object getCellEditorValue() {  
        return textArea.getText();  
    }  
  
    @Override  
    public boolean stopCellEditing() {  
        fireEditingStopped();  
        return true;  
    }  
  
    @Override  
    public boolean isCellEditable(EventObject e) {  
        if (e instanceof MouseEvent) {  
            return ((MouseEvent)e).getClickCount() >= 2;  
        }  
        return true;  
    }  

	@Override
	public void cancelCellEditing() {
		dialog.setVisible(false);
	}
  
    // Usage example:  
    public static void main(String[] args) {  
        JFrame frame = new JFrame("JTextArea TableCellEditor Example");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
        String[] columnNames = {"Text"};  
        Object[][] data = {  
            {"Row 1"},  
            {"Row 2"},  
            {"Row 3"}  
        };  
  
        JTable table = new JTable(data, columnNames);  
        table.getColumnModel().getColumn(0).setCellEditor(new JdTextAreaCellEditor());  
        // If you want to display the "Done" button, you need to create a custom TableCellRenderer  
        // table.getColumnModel().getColumn(0).setCellRenderer(new YourCustomRenderer(new JTextAreaTableCellEditor()));  
  
        JScrollPane scrollPane = new JScrollPane(table);  
        frame.add(scrollPane);  
        frame.pack();  
        frame.setVisible(true);  
    }  
}